<?php

namespace JsonLowCode\Traits;

use JsonLowCode\Helps\Random;
use JsonLowCode\PageCache;
use JsonLowCode\Axios;
use JsonLowCode\Map;
use Closure;

trait HasPageCache
{

    /**
     * @param string|null $cacheKey
     * @return string|null
     */
    public function getCacheKey(string $cacheKey = null):?string
    {
        return $cacheKey?:$this->cacheKey;
    }

    /**
     * 设置页面states数据
     * @param string $key
     * @param mixed $value
     * @param string|null $cacheKey
     * @return PageCache
     */
    public function setState(string $key, mixed $value, string $cacheKey = null): PageCache
    {
        return $this->pageCache->setState($key, $value, $this->getCacheKey($cacheKey));
    }

    /**
     * @param array $states
     * @param string|null $cacheKey
     * @return PageCache
     */
    public function setStates(array $states,?string $cacheKey = null): PageCache
    {
        return $this->pageCache->setStates($states, $this->getCacheKey($cacheKey));
    }

    /**
     * @param string $key
     * @param string|null $cacheKey
     * @return array|mixed
     */
    public function getStates(string $key = "",string $cacheKey = null): mixed
    {
        return $this->pageCache->getStates($key, $this->getCacheKey($cacheKey));
    }

    /**
     * 设置页面method方法
     * @param string $key 函数名
     * @param string $value 完整的函数
     * @param string|null $cacheKey
     * @return PageCache
     */
    public function setMethods(string $key, string $value,string $cacheKey = null): PageCache
    {
        return $this->pageCache->setMethods($key,$value, $this->getCacheKey($cacheKey));
    }

    /**
     * 获取页面methods数据
     * @param string|null $cacheKey
     * @return array
     */
    public function getMethods(string $cacheKey = null): array
    {
        return $this->pageCache->getMethods($this->getCacheKey($cacheKey));
    }

    /**
     * 设置page组件页面远程数据源
     * @param string $key 函数名
     * @param array|Axios $value
     * @param string|null $cacheKey
     * @return PageCache
     */
    public function setSourceList(string $key, array|Axios $value, string $cacheKey = null): PageCache
    {
        return $this->pageCache->setSourceList($key,$value, $this->getCacheKey($cacheKey));
    }

    /**
     * 获取page组件页面远程数据源
     * @return array
     * @param string|null $cacheKey
     */
    public function getSourceList(string $cacheKey = null): array
    {
        return $this->pageCache->getSourceList($this->getCacheKey($cacheKey));
    }

    /**
     * 设置组件映射
     * @param string $name
     * @param Map $map
     * @param string|null $cacheKey
     * @return PageCache
     */
    public function setMaps(string $name, Map $map, string $cacheKey = null): PageCache
    {
        return $this->pageCache->setMaps($name,$map, $this->getCacheKey($cacheKey));
    }

    /**
     * 获取组件映射
     * @param string|null $cacheKey
     * @return array
     */
    public function getMaps(string $cacheKey = null): array
    {
        return $this->pageCache->getMaps($this->getCacheKey($cacheKey));
    }

    /**
     * 区块组件定义
     * @param string $name 组件名称
     * @param Closure $schema
     * @param string|null $cacheKey
     * @return PageCache
     */
    public function defineBlock(string $name, Closure $schema,string $cacheKey = null): PageCache
    {
        return $this->pageCache->defineBlock($name,$schema, $this->getCacheKey($cacheKey));
    }

    /**
     * 获取定义的区块组件列表
     * @param string|null $cacheKey
     * @return array
     */
    public function getDefineBlock(string $cacheKey = null): array
    {
        return $this->pageCache->getDefineBlock($this->getCacheKey($cacheKey));
    }
}
